package com.jims.master.common.utils;



import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 打印调用
 */

public class PrintPdfUtils {

    /**
     *
     * @param userId 用户Id
     * @param jasperName ireport 文件名
     * @param paramMap 打印Page Header 内容
     * @param lists 打印 detail 内容
     * @param defaultFilename 生成文件的名称
     * @return
     */
    public static String previewPdf(String userId,String jasperName,Map paramMap,
                                    List lists, String defaultFilename) {
        HashMap<String, Object> params= new HashMap<String, Object>();
        RestUtils<Map,HashMap> restUtils =new RestUtils<Map,HashMap>();
        params.put("userId",userId);
        params.put("type","pdf");
        params.put("jasperName",jasperName);
        params.put("isPrint",true);
        params.put("paramMap",paramMap);
        params.put("lists",lists);
        params.put("defaultFilename",defaultFilename);
        Map<String ,Object> map=restUtils.findById("/ireport/view", Map.class, params,params,"");
        HashMap retMap=(HashMap)map.get("body");
        return (String)retMap.get("data");
    }

    public static void main(String[] args) throws Exception {
        Map map=new HashMap();
        map.put("test","test");
        String path=previewPdf("123456", "his/MvcIReportChsExample.jasper", map, new ArrayList(),"ee");
        System.out.print(path);
    }

    /**
     * 大字段转字符串
     *
     * @param src clob 解码
     * @return 字符串
     * @throws Exception 异常
     */
    public static String unescape(String src) {
        StringBuffer tmp = new StringBuffer();
        if (src == null || "".equals(src)) {
            return "";
        } else {
            tmp.ensureCapacity(src.length());
            int lastPos = 0, pos = 0;
            char ch;
            while (lastPos < src.length()) {
                pos = src.indexOf("%", lastPos);
                if (pos == lastPos) {
                    if (src.charAt(pos + 1) == 'u') {
                        ch = (char) Integer.parseInt(src
                                .substring(pos + 2, pos + 6), 16);
                        tmp.append(ch);
                        lastPos = pos + 6;
                    } else {
                        ch = (char) Integer.parseInt(src
                                .substring(pos + 1, pos + 3), 16);
                        tmp.append(ch);
                        lastPos = pos + 3;
                    }
                } else {
                    if (pos == -1) {
                        tmp.append(src.substring(lastPos));
                        lastPos = src.length();
                    } else {
                        tmp.append(src.substring(lastPos, pos));
                        lastPos = pos;
                    }
                }
            }
            return tmp.toString();
        }
    }
}
